package com.yuyou.fn.timer.task;

import com.github.wens.mybatisplus.examples.Example;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.educational.entity.ClassLecture;
import com.yuyou.fn.educational.entity.CourseClass;
import com.yuyou.fn.educational.entity.FinishLecture;
import com.yuyou.fn.educational.service.*;
import com.yuyou.fn.settlement.service.ISettlementService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 计算老师课酬
 * Created by wens on 2017/4/19.
 */
@JobHander(value="CalCourseTeacherCostTask")
@Component
public class CalCourseTeacherCostTask extends IJobHandler {

    private static final Logger log = LoggerFactory.getLogger(CalCourseTeacherCostTask.class);

    @Resource
    private IFinishLectureService finishLectureService ;

    @Resource
    private ISettlementService settlementService ;

    @Resource
    private ICourseClassService courseClassService ;

    @Resource
    private IClassLectureService classLectureService ;

    @Resource
    private IClassRemunerationService classRemunerationService ;

    @Resource
    private ITeacherRemunerationService teacherRemunerationService ;

    @Override
    public ReturnT<String> execute(String... params) throws Exception {


        Example<FinishLecture> finishLectureExample = FinishLecture.createExample();
        finishLectureExample.createCriteria().andEqualTo("isCalTeacherCost" , 0);

        List<FinishLecture> finishLectureList = finishLectureService.findListByExample(finishLectureExample);

        for(FinishLecture finishLecture : finishLectureList ){
            CourseClass courseClass = courseClassService.findById(finishLecture.getClassId());
            ClassLecture classLecture = classLectureService.findClassLectureByClassIdLectureNo(finishLecture.getClassId(), finishLecture.getLectureNo());

            if(classLecture.getTeacherId() == null ){
                continue;
            }

            BigDecimal amount = calRemuneration(courseClass.getAreaTeamId(),courseClass.getBusTeamId(),classLecture.getTeacherId() );
            teacherRemunerationService.addTeacherRemuneration(courseClass.getAreaTeamId(),courseClass.getBusTeamId() ,courseClass.getPeriodId(), courseClass.getClassId(),classLecture.getClassLectureId() , classLecture.getTeacherId() , 2 , amount , classLecture.getTotalAmount() ) ;

            if(amount.compareTo(BigDecimal.ZERO) != 0 ){
                String remark = "【" + courseClass.getClassName() + "】【第"+classLecture.getLectureNo()+"讲】【"+classLecture.getTeacherName()+"】薪酬" ;
                settlementService.bookTeacherCost(classLecture.getClassDate(),courseClass.getBusTeamId(),classLecture.getTeacherId(),amount,remark , courseClass.getClassId() , classLecture.getClassLectureId(),classLecture.getLectureNo(),classLecture.getTeacherId() ) ;
            }

            FinishLecture update = new FinishLecture() ;
            update.setFinishLectureId(finishLecture.getFinishLectureId());
            update.setIsCalTeacherCost(1);
            update.setUpdateTime(TimeUtils.now());
            finishLectureService.updateSelectiveById(update);

        }

        return ReturnT.SUCCESS ;

    }

    private BigDecimal calRemuneration(Long areaTeamId, Long busTeamId, Long teacherId ) {
        return classRemunerationService.calRemuneration(areaTeamId,busTeamId,teacherId , 1 );
    }



}
